//
//  AppDatabase+Msg.swift
//  chatai
//
//  Created by Macintosh on 2023/8/7.
//

import Foundation
import GRDB

extension WMRecordModel: FetchableRecord, TableRecord, PersistableRecord {
    
    static var databaseTableName: String {
        return "t_record"
    }

}

extension AppDatabase {
    
    func deleteRecord(_ recordId: String) {
        asyncWrite { db in
            try? WMRecordModel.filter(Column("recordId") == recordId).deleteAll(db)
        }
    }
    
    func deleteAllRecord() {
        asyncWrite { db in
            try? WMRecordModel.deleteAll(db)
        }
    }
    
    func getRecord(_ recordId: String) -> [WMRecordModel] {
        return (try? dbPool?.read {
            try WMRecordModel.filter(Column("recordId") == recordId).fetchAll($0)
        }) ?? []
    }
    
    func getAllRecord() -> [WMRecordModel] {
        return (try? dbPool?.read {
            try WMRecordModel.fetchAll($0)
        }) ?? []
    }

    func insertRecord(_ record: WMRecordModel) {
        asyncWrite { db in
            try record.save(db)
        }
    }
    
}
